package com.deng.netlibrary.netLib.callback;

import android.os.Handler;
import android.util.Log;

import com.deng.netlibrary.logLib.BlocklyToolkit;
import com.deng.netlibrary.netLib.model.RPCBaseModel;
import com.deng.netlibrary.netLib.response.GsonResponseHandler;
import com.deng.netlibrary.netLib.response.IResponseHandler;
import com.deng.netlibrary.netLib.response.StrResponseHandler;
import com.deng.netlibrary.netLib.utils.AESHelper;
import com.deng.netlibrary.netLib.utils.LogHelper;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


/**
 * rpc结构回调
 */

public class ZyRpcCallback implements Callback {

    private final String TAG = this.getClass().getSimpleName();

    public static void setAttention(String attention) {
        ZyRpcCallback.attention = attention;
    }

    public static String getAttention() {
        return attention;
    }

    public static String attention = "";//六足立正返回值

    private IResponseHandler mResponseHandler;
    private Handler mHandler;
    private boolean isOutputDecryption;

    public ZyRpcCallback(boolean isOutputDecryption, Handler mHandler, IResponseHandler mResponseHandler) {
        this.mHandler = mHandler;
        this.mResponseHandler = mResponseHandler;
        this.isOutputDecryption = isOutputDecryption;
    }

    @Override
    public void onFailure(Call call, final IOException e) {
//        mHandler.post(new Runnable() {
//            @Override
//            public void run() {
//                mResponseHandler.onFailure(-2, e.toString());
//            }
//        });
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        final String response_body = response.body().string();
        Log.d("niubility", "get response, body = " + response_body);

        if (response.isSuccessful()) {
            //Gson回调
            if (mResponseHandler instanceof GsonResponseHandler) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String result = AESHelper.getInstance().decryptionByAES(isOutputDecryption, response_body);
                            final RPCBaseModel rpcBaseModel = new Gson().fromJson(result, RPCBaseModel.class);
                            if (rpcBaseModel.getError() != null) {//接口层错误
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mResponseHandler.onFailure(-2, rpcBaseModel.getError().getMessage());
                                    }
                                });
                            } else {//业务层
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson = new Gson();
                                        ((GsonResponseHandler) mResponseHandler).onSuccess(0, gson.fromJson(result, ((GsonResponseHandler) mResponseHandler).getType()));
                                    }
                                });
                            }
                        } catch (Exception e) {
                            Log.d("niubility_1", "onResponse fail parse gson , body = " + response_body + "{" + e + "}");
                            ((GsonResponseHandler) mResponseHandler).onMessage(0, response_body);
//                            String userIdJiequ = response_body.substring(response_body.indexOf("\"result\": ["));
//                            userIdJiequ.substring(0, userIdJiequ.indexOf("]"));
//                            Log.d("niubility_1", "response_body  : = " + userIdJiequ.substring(11, userIdJiequ.indexOf("]")));
//                            setAttention(userIdJiequ.substring(11, userIdJiequ.indexOf("]")));
                        }
                    }
                });
            }
            else if (mResponseHandler instanceof StrResponseHandler) {
                ((StrResponseHandler) mResponseHandler).onSuccess(0, response_body);
            }
        } else {
            LogHelper.e(TAG, "onResponse fail status = " + response.code());
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mResponseHandler.onFailure(-2, "网络异常" + response.code());
                }
            });
        }
    }

    /**
     * HH 2020.11.19
     * 根据CheckBox状态传值给接口做处理；
     */
    public static class DataSynEventPerception {
        private String name;
        private boolean checked;

        public DataSynEventPerception() {
        }

        public DataSynEventPerception(String name) {
            this.name = name;

        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }



}
